<?php

namespace App\Http\Controllers\Api\Miniprogram;

use App\Http\Controllers\Controller;
use App\Models\CheckTime;
use App\Models\Order\OrderSample;
use App\Models\System\CheckItem;
use App\Models\System\TestLocation;
use App\Models\User\FamilyUser;
use Illuminate\Support\Facades\Auth;

class SampleController extends Controller
{
    public function getList()
    {
        $userId = Auth::id();
        $status = request('status');
        $checkerId = request('checker_id');
        $model = OrderSample::with(['checkItem', 'goodsOrder', 'qrcode', 'checkPdf', 'parameters.item', 'parameters.parameter', 'testLocation'])->where('binder_id', $userId)->orderByDesc('id');
        if ($status) {
            $model = $model->where('status', $status);
        } else {
            $model = $model->whereIn('status', [3, 4, 5]);
        }
        if ($checkerId) {
            $model = $model->where('checker_id', $checkerId);
        }
        $orderSamples = $model->get();
        return $this->successJson([
            'list' => $orderSamples,
            'status' => trans('order-sample.options.status'),
        ]);
    }

    public function bindUser()
    {
        $userId = Auth::id();
        $sampleSn = request('sample_sn');
        if (!$sampleSn) {
            return $this->failJson('sample.sample_sn_not_exist');
        }
        $orderSample = OrderSample::where('sample_sn', $sampleSn)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        if ($orderSample->status == 2 || $orderSample->status == 5) {
            if ($orderSample->is_used == 1 && $orderSample->binder_id == $userId) {
                return $this->successJson($orderSample);
            }
        }
        if ($orderSample->status != 1) {
            return $this->failJson('sample.sample_bind_status_error');
        }
        if ($orderSample->binder_id && $orderSample->binder_id != $userId) {
            return $this->failJson('sample.sample_has_bind');
        }
        if ($orderSample->binder_id == $userId) {
            return $this->successJson($orderSample);
        }
        $orderSample->is_used = 1;
        $orderSample->status = 2;
        $orderSample->binder_id = $userId;
        $orderSample->bind_time = new \DateTime();
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'is_used' => 1,
            'status' => 2,
            'binder_id' => $userId,
            'bind_time' => date('Y-m-d H:i:s'),
        ]);

        return $this->successJson($orderSample);
    }

    public function getTestLocations()
    {
        return $this->successJson(TestLocation::where('status', 1)->orderBy('order')->get());
    }

    public function getDetail()
    {
        $id = request('id');
        $orderSample = OrderSample::with(['qrcode', 'checkPdf', 'parameters.item', 'parameters.parameter', 'checkItem.parameters'])->where('id', $id)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        return $this->successJson($orderSample);
    }

    public function setChecker()
    {
        $userId = Auth::id();
        $id = request('id');
        $checkerId = request('checker_id');
        $orderSample = OrderSample::where('id', $id)->where('binder_id', $userId)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        $familyUser = FamilyUser::find($checkerId);
        $orderSample->checker_id = $checkerId;
        $orderSample->name = $familyUser->name;
        $orderSample->gender = $familyUser->sex;
        $orderSample->age = $familyUser->age;
        $orderSample->relationship_type = $familyUser->identity;
        $orderSample->contact = $familyUser->phone;
        $orderSample->door_phone = $familyUser->phone;
        $orderSample->id_card = $familyUser->idcard;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'checker_id' => $checkerId,
        ]);
        return $this->successJson([
            'door_phone' => $familyUser->phone,
        ]);
    }

    public function setCheckType()
    {
        $userId = Auth::id();
        $id = request('id');
        $checkType = request('check_type');
        $orderSample = OrderSample::where('id', $id)->where('binder_id', $userId)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        $orderSample->check_type = $checkType;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'check_type' => $checkType,
        ]);
        return $this->successJson();
    }

    public function setCheckTime()
    {
        $userId = Auth::id();
        $id = request('id');
        $checkTimeId = request('check_time_id');
        $orderSample = OrderSample::where('id', $id)->where('binder_id', $userId)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        $orderSample->check_time_id = $checkTimeId;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'check_time_id' => $checkTimeId,
        ]);
        return $this->successJson();
    }

    public function setCheckDate()
    {
        $userId = Auth::id();
        $id = request('id');
        $checkDate = request('check_date');
        $orderSample = OrderSample::where('id', $id)->where('binder_id', $userId)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        $orderSample->check_date = $checkDate;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'check_date' => $checkDate,
        ]);
        return $this->successJson();
    }

    public function setDoorAddress()
    {
        $userId = Auth::id();
        $id = request('id');
        $doorAddress = request('door_address');
        $orderSample = OrderSample::where('id', $id)->where('binder_id', $userId)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        $orderSample->door_address = $doorAddress;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'door_address' => $doorAddress,
        ]);
        return $this->successJson();
    }

    public function setDoorPhone()
    {
        $userId = Auth::id();
        $id = request('id');
        $doorPhone = request('door_phone');
        $orderSample = OrderSample::where('id', $id)->where('binder_id', $userId)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        $orderSample->door_phone = $doorPhone;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'door_phone' => $doorPhone,
        ]);
        return $this->successJson();
    }

    public function setTestLocation()
    {
        $userId = Auth::id();
        $id = request('id');
        $testLocationId = request('test_location_id');
        $orderSample = OrderSample::where('id', $id)->where('binder_id', $userId)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        $orderSample->test_location_id = $testLocationId;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'test_location_id' => $testLocationId,
        ]);
        return $this->successJson();
    }

    public function writeOff()
    {
        $sampleSn = request('sample_sn');
        if (!$sampleSn) {
            return $this->failJson('sample.sample_sn_not_exist');
        }
        $orderSample = OrderSample::where('sample_sn', $sampleSn)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        if ($orderSample->status != 2) {
            return $this->failJson('sample.sample_bind_status_error');
        }

        if ($orderSample->check_type == 0) {
            return $this->failJson('sample.sample_check_type_not_exist');
        }
        if ($orderSample->check_type == 1 && $orderSample->test_location_id == 0) {
            return $this->failJson('sample.sample_test_location_not_exist');
        }
        if ($orderSample->checker_id == 0) {
            return $this->failJson('sample.sample_checker_not_exist');
        }
        if (trim($orderSample->check_date) == '') {
            return $this->failJson('sample.sample_check_date_not_exist');
        }
        if ($orderSample->check_time_id == 0) {
            return $this->failJson('sample.sample_check_time_not_exist');
        }
        if (trim($orderSample->door_phone) == '') {
            return $this->failJson('sample.sample_door_phone_not_exist');
        }
        if ($orderSample->check_type == 2 && trim($orderSample->door_address) == '') {
            return $this->failJson('sample.sample_door_address_not_exist');
        }

        $orderSample->status = 5;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'status' => 5,
        ]);

        return $this->successJson();
    }

    public function scanWriteOff()
    {
        $sampleSn = request('sample_sn');
        if (!$sampleSn) {
            return $this->failJson('sample.sample_sn_not_exist');
        }
        $orderSample = OrderSample::where('sample_sn', $sampleSn)->first();
        if (!$orderSample) {
            return $this->failJson('sample.sample_not_exist');
        }
        if ($orderSample->status != 5) {
            return $this->failJson('sample.sample_scan_status_error');
        }
        $orderSample->status = 3;
        $orderSample->save();

        $orderSample->saveOperateLog(2, [
            'status' => 3,
        ]);

        return $this->successJson();
    }
}
